package com.levi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.levi.constants.MallConstants;
import com.levi.enums.EnumUtil;
import com.levi.mall.mapper.CategoryMapper;
import com.levi.mall.service.CategoryService;
import com.levi.pojo.Category;
import com.levi.vo.CategoryVo;
import com.levi.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shentong
 * @since 2022/3/15 8:57 PM
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {


    @Override
    public ResponseVo<List<CategoryVo>> seletAll() {
        //要返回的categoryVoList
        List<Category> categories = getAllCategoryList();
        //查询出parent_id = 0的类目
        List<CategoryVo> categoryVoList = categories.stream().filter(category -> Objects.equals(category.getParentId(),MallConstants.ROOT_ID))
                .map(this::category2CategoryVo)
                .collect(Collectors.toList());

        findSubCategory(categories, categoryVoList);
        return ResponseVo.success(categoryVoList);
    }

    /**
     * 获取所有的商品类目列表
     * @return categories
     */
    private List<Category> getAllCategoryList() {
        LambdaQueryWrapper<Category> ew = new LambdaQueryWrapper<>();
        ew.eq(Category::getStatus, EnumUtil.STATUS_NOMAL.getCode())
                .orderByDesc(Category::getSortOrder);
        return this.list(ew);
    }


    @Override
    public void findSubCategoryId(Integer categoryId, Set<Integer> categoryIdSet) {
        List<Category> allCategoryList = getAllCategoryList();
        findSubCategoryId(categoryId,categoryIdSet,allCategoryList);
    }


    /**
     * 递归查询categoryId下面所有的类目id
     * @param categoryId
     * @param categoryIdSet
     * @param allCategoryList
     */
    private void findSubCategoryId(Integer categoryId, Set<Integer> categoryIdSet,List<Category> allCategoryList) {
        for (Category category : allCategoryList) {
            if (Objects.equals(category.getParentId(),categoryId)) {
                categoryIdSet.add(category.getId());
                findSubCategoryId(category.getId(),categoryIdSet,allCategoryList);
            }
        }
    }



    /**
     * 递归查询子类目
     * @param categories
     * @param categoryVoList
     */
    private void findSubCategory(List<Category> categories, List<CategoryVo> categoryVoList) {
        for (CategoryVo categoryVo : categoryVoList) {
            List<CategoryVo> categoryVos = new ArrayList<>();
            for (Category category : categories) {
                if (Objects.equals(categoryVo.getId(),category.getParentId())) {
                    categoryVos.add(category2CategoryVo(category));
                }
            }
            //子类目排序
            List<CategoryVo> sortedCategoryVos = categoryVos.stream().sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed()).collect(Collectors.toList());
            categoryVo.setSubCateories(sortedCategoryVos);
            //此时parentId = 0的类目的子类目已经设置完毕，解析来递归为parentId = 0的类目的子类目设置子类目
            findSubCategory(categories,categoryVos);
        }

    }

    /**
     * 类型转换
     * @param category
     * @return categoryVo
     */
    private CategoryVo category2CategoryVo(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category, categoryVo);
        return categoryVo;
    }


}
